package com.getpebble.android.bluetooth.device;

import android.content.Context;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import com.getpebble.android.bluetooth.radio.PebbleBluetoothDevice.BondInitFailedException;
import com.getpebble.android.bluetooth.receiver.BondReceiver;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

abstract class RemoteDeviceConnectTask extends PblAsyncTask {
    public static final String TAG = RemoteDeviceConnectTask.class.getSimpleName();
    private Runnable closeConnectionResourcesTimeoutRunnable = new Runnable() {
        public void run() {
            Trace.info(RemoteDeviceConnectTask.TAG, "Blocking connect timeout cleanup timeout");
            RemoteDeviceConnectTask.this.cancel();
            RemoteDeviceConnectTask.this.mFailureReason = ConnectionResult.TIMEOUT;
        }
    };
    BondReceiver mBondReceiver = null;
    private WeakReference<Context> mContext = null;
    private CountDownLatch mCountDownLatch = new CountDownLatch(1);
    private ConnectionResult mFailureReason;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private boolean mIsCancelling = false;
    private boolean mPairFinished = false;
    private long mPairTimeoutMs = 30000;
    private RemoteDevice mRemoteDevice = null;
    private Runnable mTimeoutRunnable = new Runnable() {
        public void run() {
            Trace.info(RemoteDeviceConnectTask.TAG, "Blocking connect timeout");
            RemoteDeviceConnectTask.this.mHandler.postDelayed(RemoteDeviceConnectTask.this.closeConnectionResourcesTimeoutRunnable, 2000);
            RemoteDeviceConnectTask.this.mRemoteDevice.killSocket();
        }
    };

    class DodgyPairingException extends Exception {
        DodgyPairingException() {
        }
    }

    abstract void onConnectTaskFailure(ConnectionResult connectionResult);

    abstract void onConnectTaskSuccess();

    private static boolean requiresExplicitBonding() {
        return VERSION.SDK_INT >= 17;
    }

    private static boolean requiresPostBondDelay() {
        return VERSION.SDK_INT >= 19;
    }

    private static boolean shouldCancelDiscovery() {
        return VERSION.SDK_INT < 17;
    }

    private static boolean shouldDoBadPairingUnpairing() {
        return VERSION.SDK_INT >= 18;
    }

    RemoteDeviceConnectTask(Context context, RemoteDevice remoteDevice) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        } else if (remoteDevice == null) {
            throw new IllegalArgumentException("'remoteDevice' cannot be null!");
        } else {
            this.mRemoteDevice = remoteDevice;
            this.mContext = new WeakReference(context);
        }
    }

    public synchronized void cancel() {
        if (this.mIsCancelling) {
            Trace.info(TAG, "cancel(); already cancelling");
        } else {
            Trace.debug(TAG, "cancel()");
            this.mIsCancelling = true;
        }
    }

    private void createNewBondReceiver() {
        Context context = (Context) this.mContext.get();
        if (context == null) {
            Trace.warning(TAG, "createNewBondReceiver: context is null");
        } else if (this.mBondReceiver != null) {
            Trace.warning(TAG, "createNewBondReceiver: mBondReceiver is not null");
        } else {
            this.mBondReceiver = new BondReceiver(context) {
                public void onDeviceBonded(String address) {
                    Trace.verbose(RemoteDeviceConnectTask.TAG, "onDeviceBonded: " + address);
                    RemoteDeviceConnectTask.this.mPairFinished = true;
                    RemoteDeviceConnectTask.this.mCountDownLatch.countDown();
                }

                public void onDeviceUnbonded(String address) {
                    Trace.verbose(RemoteDeviceConnectTask.TAG, "onDeviceUnbonded: " + address);
                    RemoteDeviceConnectTask.this.mPairFinished = false;
                    RemoteDeviceConnectTask.this.mCountDownLatch.countDown();
                }

                public void onDevicePairingRequest(String address) {
                    if (RemoteDeviceConnectTask.this.mRemoteDevice.getAddress().equals(address)) {
                        Trace.debug(RemoteDeviceConnectTask.TAG, "Pairing request received; accepting");
                        RemoteDeviceConnectTask.this.mRemoteDevice.acceptPairingRequest();
                        return;
                    }
                    Trace.verbose(RemoteDeviceConnectTask.TAG, "pairing request received for wrong device: " + address);
                }
            };
        }
    }

    public boolean doInBackground() {
        boolean res = doConnectWithDiagnosisUnpairRetry(true);
        if (!this.mIsCancelling) {
            return res;
        }
        Trace.debug(TAG, "Returning failure because cancelled");
        if (this.mFailureReason == null) {
            this.mFailureReason = ConnectionResult.NOT_AVAILABLE;
        }
        return false;
    }

    private boolean doBondingProcess() {
        boolean waitForBond;
        try {
            waitForBond = this.mRemoteDevice.createBond();
        } catch (BondInitFailedException e) {
            try {
                Trace.debug(TAG, "Bond init failed once; retry bond init after delay");
                Thread.sleep(1000);
                waitForBond = this.mRemoteDevice.createBond();
            } catch (BondInitFailedException e2) {
                Trace.warning(TAG, "Bond init failed twice", e2);
                this.mFailureReason = ConnectionResult.NOT_BONDED;
                return false;
            } catch (InterruptedException e22) {
                Trace.warning(TAG, "Interrupted during bond retry delay sleep", e22);
                this.mFailureReason = ConnectionResult.NOT_BONDED;
                return false;
            }
        }
        if (waitForBond) {
            Trace.debug(TAG, "Need to wait on the bond!");
            try {
                this.mCountDownLatch.await(this.mPairTimeoutMs, TimeUnit.MILLISECONDS);
                Trace.debug(TAG, "Bond wait complete");
            } catch (InterruptedException e3) {
                Trace.error(TAG, "bonding latch interrupted", e3);
            }
            if (!this.mPairFinished) {
                Trace.warning(TAG, "Pairing not finished; abort connection");
                this.mRemoteDevice.fireCallback(RemoteDeviceEvent.UNBONDED);
                this.mFailureReason = ConnectionResult.NOT_BONDED;
                return false;
            }
        }
        if (!this.mRemoteDevice.isBonded()) {
            Trace.warning(TAG, "Device not bonded - this is not expected at this point - wait then retry the check");
            try {
                Thread.sleep(1000);
                if (!this.mRemoteDevice.isBonded()) {
                    Trace.warning(TAG, "Device not bonded after 2nd check");
                    this.mFailureReason = ConnectionResult.NOT_BONDED;
                    return false;
                }
            } catch (InterruptedException e222) {
                Trace.warning(TAG, "Interrupted during bond check retry delay sleep", e222);
                this.mFailureReason = ConnectionResult.NOT_BONDED;
                return false;
            }
        }
        if (waitForBond && requiresPostBondDelay()) {
            Trace.debug(TAG, "KitKat post-bond hack sleep...");
            try {
                Thread.sleep(500);
                Trace.debug(TAG, "Finished kitkat post-bond delay");
            } catch (InterruptedException e4) {
                Trace.info(TAG, "Interrupted during post-bond delay");
                return false;
            }
        }
        return true;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private boolean doConnectWithDiagnosisUnpairRetry(boolean r13) {
        /*
        r12 = this;
        r8 = TAG;
        r9 = new java.lang.StringBuilder;
        r9.<init>();
        r10 = "doConnectWithDiagnosisUnpairRetry: firstAttempt = ";
        r9 = r9.append(r10);
        r9 = r9.append(r13);
        r9 = r9.toString();
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);
        r12.createNewBondReceiver();
        r8 = requiresExplicitBonding();
        if (r8 == 0) goto L_0x0030;
    L_0x0021:
        r2 = r12.doBondingProcess();
        if (r2 != 0) goto L_0x0030;
    L_0x0027:
        r8 = TAG;
        r9 = "Bonding process failed; aborting";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);
        r8 = 0;
    L_0x002f:
        return r8;
    L_0x0030:
        r8 = r12.mRemoteDevice;	 Catch:{ IOException -> 0x00e8 }
        r8.createSocket();	 Catch:{ IOException -> 0x00e8 }
        r8 = com.getpebble.android.bluetooth.device.AbsDeviceService.getPebbleBluetoothAdapter();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r8.isDiscovering();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        if (r8 == 0) goto L_0x007f;
    L_0x003f:
        r8 = shouldCancelDiscovery();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        if (r8 == 0) goto L_0x007f;
    L_0x0045:
        r8 = TAG;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = "Currently discovering: cancel discovery";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = com.getpebble.android.bluetooth.device.AbsDeviceService.getPebbleBluetoothAdapter();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8.cancelDiscovery();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r0 = java.lang.System.currentTimeMillis();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = TAG;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = "Waiting for discovery to finish...";
        com.getpebble.android.common.core.trace.Trace.verbose(r8, r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
    L_0x005e:
        r8 = com.getpebble.android.bluetooth.device.AbsDeviceService.getPebbleBluetoothAdapter();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r8.isDiscovering();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        if (r8 == 0) goto L_0x007f;
    L_0x0068:
        r8 = 100;
        java.lang.Thread.sleep(r8);	 Catch:{ InterruptedException -> 0x0103 }
        r8 = java.lang.System.currentTimeMillis();	 Catch:{ InterruptedException -> 0x0103 }
        r8 = r8 - r0;
        r10 = 15000; // 0x3a98 float:2.102E-41 double:7.411E-320;
        r8 = (r8 > r10 ? 1 : (r8 == r10 ? 0 : -1));
        if (r8 <= 0) goto L_0x005e;
    L_0x0078:
        r8 = TAG;	 Catch:{ InterruptedException -> 0x0103 }
        r9 = "Timed out waiting for discovery to finish";
        com.getpebble.android.common.core.trace.Trace.info(r8, r9);	 Catch:{ InterruptedException -> 0x0103 }
    L_0x007f:
        r8 = TAG;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = new java.lang.StringBuilder;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9.<init>();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r10 = "Posting connect timeout for ";
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r10 = r12.getBlockingConnectTimeoutMs();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r10 = " ms";
        r9 = r9.append(r10);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = r9.toString();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        com.getpebble.android.common.core.trace.Trace.verbose(r8, r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r12.mHandler;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = r12.mTimeoutRunnable;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r10 = r12.getBlockingConnectTimeoutMs();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8.postDelayed(r9, r10);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r12.mRemoteDevice;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8.connectToSocket();	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = TAG;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = "Connected; removing timeout";
        com.getpebble.android.common.core.trace.Trace.verbose(r8, r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r12.mRemoteDevice;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = 0;
        r8.mOneDodgyPairingExceptionCounted = r9;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r12.mHandler;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = r12.mTimeoutRunnable;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8.removeCallbacks(r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = r12.mHandler;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = r12.closeConnectionResourcesTimeoutRunnable;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8.removeCallbacks(r9);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r8 = 1;
        r9 = r12.mHandler;
        r10 = r12.mTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mHandler;
        r10 = r12.closeConnectionResourcesTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mBondReceiver;
        if (r9 == 0) goto L_0x002f;
    L_0x00de:
        r9 = r12.mBondReceiver;
        r9.stopReceiving();
        r9 = 0;
        r12.mBondReceiver = r9;
        goto L_0x002f;
    L_0x00e8:
        r5 = move-exception;
        r8 = TAG;
        r9 = "Error creating socket";
        com.getpebble.android.common.core.trace.Trace.error(r8, r9, r5);
        r8 = r12.mRemoteDevice;	 Catch:{ IOException -> 0x00fc }
        r8.closeConnectionResources();	 Catch:{ IOException -> 0x00fc }
    L_0x00f5:
        r8 = com.getpebble.android.bluetooth.device.ConnectionResult.NOT_AVAILABLE;
        r12.mFailureReason = r8;
        r8 = 0;
        goto L_0x002f;
    L_0x00fc:
        r6 = move-exception;
        r8 = TAG;
        com.getpebble.android.common.core.trace.Trace.error(r8, r6);
        goto L_0x00f5;
    L_0x0103:
        r5 = move-exception;
        r8 = TAG;	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        r9 = "Interrupted waiting for discovery to finish";
        com.getpebble.android.common.core.trace.Trace.error(r8, r9, r5);	 Catch:{ IOException -> 0x010d, NullPointerException -> 0x0204 }
        goto L_0x005e;
    L_0x010d:
        r3 = move-exception;
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "Could not connect to socket";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9, r3);	 Catch:{ all -> 0x0159 }
        r8 = r12.mHandler;	 Catch:{ all -> 0x0159 }
        r9 = r12.mTimeoutRunnable;	 Catch:{ all -> 0x0159 }
        r8.removeCallbacks(r9);	 Catch:{ all -> 0x0159 }
        r8 = r12.mHandler;	 Catch:{ all -> 0x0159 }
        r9 = r12.closeConnectionResourcesTimeoutRunnable;	 Catch:{ all -> 0x0159 }
        r8.removeCallbacks(r9);	 Catch:{ all -> 0x0159 }
        r8 = r12.mRemoteDevice;	 Catch:{ IOException -> 0x0150, NullPointerException -> 0x0175 }
        r8.closeConnectionResources();	 Catch:{ IOException -> 0x0150, NullPointerException -> 0x0175 }
    L_0x0128:
        r12.diagnoseConnectionFailure(r3);	 Catch:{ DodgyPairingException -> 0x017e }
    L_0x012b:
        r8 = r12.mHandler;
        r9 = r12.mTimeoutRunnable;
        r8.removeCallbacks(r9);
        r8 = r12.mHandler;
        r9 = r12.closeConnectionResourcesTimeoutRunnable;
        r8.removeCallbacks(r9);
        r8 = r12.mBondReceiver;
        if (r8 == 0) goto L_0x0145;
    L_0x013d:
        r8 = r12.mBondReceiver;
        r8.stopReceiving();
        r8 = 0;
        r12.mBondReceiver = r8;
    L_0x0145:
        r8 = r12.mFailureReason;
        if (r8 != 0) goto L_0x014d;
    L_0x0149:
        r8 = com.getpebble.android.bluetooth.device.ConnectionResult.NOT_AVAILABLE;
        r12.mFailureReason = r8;
    L_0x014d:
        r8 = 0;
        goto L_0x002f;
    L_0x0150:
        r5 = move-exception;
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "Error disconnecting";
        com.getpebble.android.common.core.trace.Trace.error(r8, r9, r5);	 Catch:{ all -> 0x0159 }
        goto L_0x0128;
    L_0x0159:
        r8 = move-exception;
        r9 = r12.mHandler;
        r10 = r12.mTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mHandler;
        r10 = r12.closeConnectionResourcesTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mBondReceiver;
        if (r9 == 0) goto L_0x0174;
    L_0x016c:
        r9 = r12.mBondReceiver;
        r9.stopReceiving();
        r9 = 0;
        r12.mBondReceiver = r9;
    L_0x0174:
        throw r8;
    L_0x0175:
        r7 = move-exception;
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "Error disconnecting";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9, r7);	 Catch:{ all -> 0x0159 }
        goto L_0x0128;
    L_0x017e:
        r4 = move-exception;
        r8 = r12.mRemoteDevice;	 Catch:{ all -> 0x0159 }
        r8 = r8.mOneDodgyPairingExceptionCounted;	 Catch:{ all -> 0x0159 }
        if (r8 == 0) goto L_0x01f6;
    L_0x0185:
        r8 = r12.mRemoteDevice;	 Catch:{ all -> 0x0159 }
        r9 = 0;
        r8.mOneDodgyPairingExceptionCounted = r9;	 Catch:{ all -> 0x0159 }
        if (r13 == 0) goto L_0x012b;
    L_0x018c:
        r8 = shouldDoBadPairingUnpairing();	 Catch:{ all -> 0x0159 }
        if (r8 == 0) goto L_0x012b;
    L_0x0192:
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "BPE: removing bond then retrying";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);	 Catch:{ all -> 0x0159 }
        r8 = r12.mRemoteDevice;	 Catch:{ all -> 0x0159 }
        r8 = r8.removeBond();	 Catch:{ all -> 0x0159 }
        if (r8 == 0) goto L_0x01ed;
    L_0x01a1:
        r8 = 500; // 0x1f4 float:7.0E-43 double:2.47E-321;
        java.lang.Thread.sleep(r8);	 Catch:{ InterruptedException -> 0x01c7 }
        r8 = 0;
        r8 = r12.doConnectWithDiagnosisUnpairRetry(r8);	 Catch:{ all -> 0x0159 }
        r9 = r12.mHandler;
        r10 = r12.mTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mHandler;
        r10 = r12.closeConnectionResourcesTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mBondReceiver;
        if (r9 == 0) goto L_0x002f;
    L_0x01bd:
        r9 = r12.mBondReceiver;
        r9.stopReceiving();
        r9 = 0;
        r12.mBondReceiver = r9;
        goto L_0x002f;
    L_0x01c7:
        r5 = move-exception;
        r8 = r12.mFailureReason;	 Catch:{ all -> 0x0159 }
        if (r8 != 0) goto L_0x01d0;
    L_0x01cc:
        r8 = com.getpebble.android.bluetooth.device.ConnectionResult.NOT_AVAILABLE;	 Catch:{ all -> 0x0159 }
        r12.mFailureReason = r8;	 Catch:{ all -> 0x0159 }
    L_0x01d0:
        r8 = 0;
        r9 = r12.mHandler;
        r10 = r12.mTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mHandler;
        r10 = r12.closeConnectionResourcesTimeoutRunnable;
        r9.removeCallbacks(r10);
        r9 = r12.mBondReceiver;
        if (r9 == 0) goto L_0x002f;
    L_0x01e3:
        r9 = r12.mBondReceiver;
        r9.stopReceiving();
        r9 = 0;
        r12.mBondReceiver = r9;
        goto L_0x002f;
    L_0x01ed:
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "removeBond failed; not retrying...";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);	 Catch:{ all -> 0x0159 }
        goto L_0x012b;
    L_0x01f6:
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "BPE: let CSM retry once";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9);	 Catch:{ all -> 0x0159 }
        r8 = r12.mRemoteDevice;	 Catch:{ all -> 0x0159 }
        r9 = 1;
        r8.mOneDodgyPairingExceptionCounted = r9;	 Catch:{ all -> 0x0159 }
        goto L_0x012b;
    L_0x0204:
        r7 = move-exception;
        r8 = TAG;	 Catch:{ all -> 0x0159 }
        r9 = "Failed to connect to device";
        com.getpebble.android.common.core.trace.Trace.debug(r8, r9, r7);	 Catch:{ all -> 0x0159 }
        r8 = r12.mHandler;
        r9 = r12.mTimeoutRunnable;
        r8.removeCallbacks(r9);
        r8 = r12.mHandler;
        r9 = r12.closeConnectionResourcesTimeoutRunnable;
        r8.removeCallbacks(r9);
        r8 = r12.mBondReceiver;
        if (r8 == 0) goto L_0x0145;
    L_0x021e:
        r8 = r12.mBondReceiver;
        r8.stopReceiving();
        r8 = 0;
        r12.mBondReceiver = r8;
        goto L_0x0145;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.getpebble.android.bluetooth.device.RemoteDeviceConnectTask.doConnectWithDiagnosisUnpairRetry(boolean):boolean");
    }

    protected void diagnoseConnectionFailure(IOException e) throws DodgyPairingException {
        try {
            StackTraceElement[] elements = e.getStackTrace();
            if (elements[1].getMethodName().equals("waitSocketSignal")) {
                Trace.debug(TAG, "> diagnoseConnectionFailure: dodgy pairing");
                throw new DodgyPairingException();
            } else if (elements[1].getMethodName().equals("readInt")) {
                Trace.debug(TAG, "> diagnoseConnectionFailure: device not available");
            } else if (elements[0].getMethodName().equals("connectNative")) {
                Trace.debug(TAG, "> diagnoseConnectionFailure: device not available (after SDP workaround)");
            } else {
                Trace.debug(TAG, "> diagnoseConnectionFailure: unknown: -");
                for (int i = 0; i < elements.length; i++) {
                    Trace.debug(TAG, "> " + i + ": " + elements[i].getMethodName() + " (" + elements[i].getLineNumber() + ")");
                }
            }
        } catch (DodgyPairingException bpe) {
            throw bpe;
        } catch (Exception e2) {
            Trace.debug(TAG, "> diagnoseConnectionFailure failed internally: ", e);
        }
    }

    public void onTaskSuccess() {
        onConnectTaskSuccess();
    }

    public void onTaskFailed() {
        Trace.debug(TAG, "Failed: reason = " + this.mFailureReason);
        onConnectTaskFailure(this.mFailureReason);
    }

    protected long getBlockingConnectTimeoutMs() {
        return 45000;
    }
}
